<template>
  <div
    ref="bt"
    :class="className"
    :style="{ height: height, width: width }"
  />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],

  props: {
    chartData: {
      type: Object,
      required: true
    },

    className: {
      type: String,
      default: 'bt'
    },
    id: {
      type: String,
      default: 'bt'
    },
    width: {
      type: String,
      default: '750px'
    },
    height: {
      type: String,
      default: '400px'
    }
  },
  data() {
    return {
      chart: null,
      op: {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            name: this.chartData.name,
            data: this.chartData.data
          }
        ]
      }
    }
  },
  watch: {
    chartData(newV, oldV) {
      this.initChart()
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      require('./walden.js')
      this.chart = echarts.init(this.$refs.bt, 'walden')
      this.op.series[0].name = this.chartData.name
      this.op.series[0].data = this.chartData.data
      this.chart.setOption(this.op)
    }
  }
}
</script>
